<?php include 'header.php'
/*
whmtools - easily find accounts on cpanels servers
Copyright (C) 2006  Matthew Davis <matthew@familycampground.org>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
?>
<title>Search</title>
<body>
<div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
<form action="search.php" method="post">
    <input type="text" size=50 name=searchFor value=<?php echo $searchFor?>>
    <select name="searchBy">
        <option value="domain" <?php if ($searchBy == domain) print "selected"?>>Domain</option>
        <option value="server" <?php if ($searchBy == server) print "selected"?>>Server</option>
        <option value="account" <?php if ($searchBy == account) print "selected"?>>Username</option>
        <option value="owner" <?php if ($searchBy == owner) print "selected"?>>Owner</option>
        <option value="package" <?php if ($searchBy == package) print "selected"?>>Package</option>
        <option value="realIP" <?php if ($searchBy == realIP) print "selected"?>>IP</option>
        <option value="skin" <?php if ($searchBy == skin) print "selected"?>>Skin</option>
        <option value="quota" <?php if ($searchBy == quota) print "selected"?>>Quota</option>
        <option value="dateCreated" <?php if ($searchBy == dateCreated) print "selected"?>>Date Created</option>
    </select>
    <input type="submit" value="Search">
    <br><i>Note: Use % as a wildcard.
    <br>Ex: To find all domains with 'ivalue' in it, use '%ivalue%' and select domain.</i>
</form>
<?php

// Initialize a vlue For alternating colors per line
$value = 0;

// Catch *'s and switch them to %'s which is the sql wildcard.
$searchFor = str_replace("*","%",$searchFor);

require 'config.php';

mysql_select_db ("whmtools");
if (!$searchFor && !$sortBy)
    $result = mysql_query("SELECT * from accounts");
elseif (!serachFor && $sortBy)
    $result = mysql_query("SELECT * from accounts ORDER BY `$sortBy`");
elseif ($sortBy)
    $result = mysql_query("SELECT * FROM `accounts` WHERE `$searchBy` LIKE '%$searchFor%' ORDER BY `$sortBy`");
else
    $result = mysql_query("SELECT * FROM `accounts` WHERE `$searchBy` LIKE '%$searchFor%'");
print "Total results: " . mysql_num_rows($result) . "<br>\n";
print "<table border=1>\n";
print "\t<th><a href=\"search.php?sortBy=server&searchBy=$searchBy&searchFor=$searchFor\">Server</th>
        <th><a href=\"search.php?sortBy=account&searchBy=$searchBy&searchFor=$searchFor\">UserName</a></th>
        <th><a href=\"search.php?sortBy=domain&searchBy=$searchBy&searchFor=$searchFor\">Domain</a> <img src=\"images/exclamation.gif\"></th>
        <th><a href=\"search.php?sortBy=package&searchBy=$searchBy&searchFor=$searchFor\">Package</a></th>
        <th><a href=\"search.php?sortBy=owner&searchBy=$searchBy&searchFor=$searchFor\">Owner</a></th>
	<th><a href=\"search.php?sortBy=realIP&searchBy=$searchBy&searchFor=$searchFor\">IP on server</a></th>
	<th><a href=\"search.php?sortBy=skin&searchBy=$searchBy&searchFor=$searchFor\">Skin</a></th>
	<th><a href=\"search.php?sortBy=quota&searchBy=$searchBy&searchFor=$searchFor\">Quota</a></th>
	<th><a href=\"search.php?sortBy=dateCreated&searchBy=$searchBy&searchFor=$searchFor\">Date Created</a></th>
	<th>Cpanel</th><th>WHM</th>\n";
while ($matches = mysql_fetch_row($result)) {
    // $matches holds a row for each account
    // and below is what the value is according
    // to the coorsponding index
    // 0 - id
    // 1 - server
    // 2 - account
    // 3 - domain
    // 4 - package
    // 5 - owner
    // 6 - realIP
    // 7 - skin
    // 8 - quota
    // 9 - date account was created
    // 10 - actual IP of hostname
    
    // QUOTA CHECKING
    // --------------
    // The quota value in the sql database is an int value
    // If the quota limit is zero, it's actually unlimited
    if ($matches[8] == 0) $matches[8] = "unlimited";
    // Then finally add 'meg' onto the output for readability
    else $matches[8] = $matches[8]/1024 . " meg";
    
    // ACTUAL IP CHECKING
    // ------------------
    // If no acutal IP is showing up (NULL value)
    // it means the resolvehost.php script has not been
    // run to update the database
//    if ($matches[10] == NULL) $matches[10] = "<a href=\"resolvehosts.php\">Update</a>";
    
    print "\t<tr ";
    // Alternating colors per line
    if ($value == 1) print "bgcolor=\"#f5f5f5\">";
    else print "bgcolor=\"#ffefd5\">";
    $value = !($value);
    
    print "<td>$matches[1]</td>
	    <td><a href=\"http://$matches[6]/~$matches[2]/\">$matches[2]</a></td>
	    <td><a href=\"http://$matches[3]\" onmouseover=\"return overlib('$matches[10]');\" onmouseout=\"return nd();\">$matches[3]</a> <a href=\"http://www.dnsstuff.com/tools/whois.ch?ip=$matches[3]\"><img src=\"images/icon_lookup.gif\" alt=\"Whois\" border=0></a> <a href=\"http://www.dnsstuff.com/tools/lookup.ch?name=$matches[3]&type=A\" title=\"DNS Lookup\">D</a></td>
	    <td>$matches[4]</td>
	    <td>$matches[5]</td>";

    // Check to see if the IP assigned to the site
    // is really the IP address the domain resovles to
    // If its not, make it stand out
    switch ($matches[12]) {
     case 0:
	print "<td>$matches[10]</td>\n";
	break;
     case 1:
	print "<td><a href=\"#\" onclick=\"javascript:window.open('ipquery.php?realIP=$matches[6]&actualIP=$matches[10]', 'resolve', 'resizeable=yes,scrollbars=yes,width=800,height=200');\">$matches[6]</a></td>\n";
	break;
     case 2:
	print "<td bgcolor=\"#ffa380\"><a href=\"#\" onclick=\"javascript:window.open('ipquery.php?realIP=$matches[6]&actualIP=$matches[10]', 'resolve', 'resizeable=yes,scrollbars=yes,width=800,height=200');\">$matches[6]</a></td>\n";
	break;
    }
    print "\t<td>$matches[7]</td>
	    <td>$matches[8]</td>\n";
    print "\t<td>" . date("j M y g:i:s",$matches[9]) . "</td>
	    <td align=center><a href=\"https://$matches[6]:2083\"><img src=\"images/cpanel.gif\" alt=Cpanel></a></td>
	    <td align=center><a href=\"https://$matches[6]:2087\"><img src=\"images/transfers.gif\" alt=WHM></a></td></tr>\n";
}
print "</table>\n";
?>
</html>
